<?php
/**
 * 提现model
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */


class b2c_mdl_integralfreightReport extends dbeav_model{



    // var $defaultOrder = array('m.member_id', ' ASC');
    // 关联dbschema
    function __construct($app)
    {
        parent::__construct($app);
        $this->app = $app;

        $officemodel=app::get('ectools')->model('office');
        $officedata=$officemodel->getList('*');
        $office_id=array();
        for ($i=0; $i <count($officedata) ; $i++) { 
            $office_id[$officedata[$i]['office_id']]=$officedata[$i]['local_name'];
        }

        if(!$office_id)
        {
            $office_id=0;
        }

        $columns=array(
                    'member_name'=>array('label'=>app::get('b2c')->_('渠道商用户名')
                        // ,'searchtype' => 'has'
                        ,'filtertype' => 'normal','filterdefault' => 'true'
                        ,'width'=>100),
                    'u8code'=>array('label'=>app::get('b2c')->_('u8编号')
                        // ,'searchtype' => 'has'
                        ,'filtertype' => 'normal','filterdefault' => 'true','width'=>100),

                     'office_id'=>array('label'=>app::get('b2c')->_('办事处'),'filtertype' => 'normal','filterdefault' => 'true','width'=>80,'type' =>$office_id),

                    'createtime'=>array('label'=>app::get('b2c')->_('订单日期'),'width'=>80),
                    'order_id'=>array('label'=>app::get('b2c')->_('订单号')
                        // ,'searchtype' => 'has'
                        ,'filtertype' => 'normal','filterdefault' => 'true','width'=>150),
                    'cost_item'=>array('label'=>app::get('b2c')->_('总订单金额(不含运费、积分扣减)'),'width'=>100),
                    'order_chgpointscore'=>array('label'=>app::get('b2c')->_('本订单所使用积分'),'width'=>100),
                    'order_chgpointmoney'=>array('label'=>app::get('b2c')->_('本订单积分兑换金额'),'width'=>100),
                    'cost_freight'=>array('label'=>app::get('b2c')->_('本订单运费'),'width'=>100),
                    'pay_status'=>array('label'=>app::get('b2c')->_('支付状态'),'filtertype' => 'normal','filterdefault' => 'true','width'=>100,'type' =>
                      array (
                        0 => app::get('b2c')->_('未支付'),
                        1 => app::get('b2c')->_('已支付'),
                        2 => app::get('b2c')->_('已付款至到担保方'),
                        3 => app::get('b2c')->_('部分付款'),
                        4 => app::get('b2c')->_('部分退款'),
                        5 => app::get('b2c')->_('全额退款'),
                      )
                      ),
                    );

        $this->columns = array(
                    'member_name'=>array('label'=>app::get('b2c')->_('渠道商用户名')
                        // ,'searchtype' => 'has'
                        ,'filtertype' => 'normal','filterdefault' => 'true'
                        ,'width'=>100),
                    'u8code'=>array('label'=>app::get('b2c')->_('u8编号')
                        // ,'searchtype' => 'has'
                        ,'filtertype' => 'normal','filterdefault' => 'true','width'=>100),
                    'office_id'=>array('label'=>app::get('b2c')->_('办事处'),'filtertype' => 'normal','filterdefault' => 'true','width'=>80,'type' =>$office_id),

                    'createtime'=>array('label'=>app::get('b2c')->_('订单日期'),'width'=>80),
                    'createstarttime'=>array('label'=>app::get('b2c')->_('订单日期从...'),'filtertype' => 'normal','filterdefault' => 'true','type'=>'date'),
                    'createendtime'=>array('label'=>app::get('b2c')->_('到...'),'filtertype' => 'normal','filterdefault' => 'true','type'=>'date'),
                    'order_id'=>array('label'=>app::get('b2c')->_('订单号')
                        // ,'searchtype' => 'has'
                        ,'filtertype' => 'normal','filterdefault' => 'true','width'=>150),
                    'cost_item'=>array('label'=>app::get('b2c')->_('总订单金额(不含运费、积分扣减)'),'width'=>100),
                    'order_chgpointscore'=>array('label'=>app::get('b2c')->_('本订单所使用积分'),'width'=>100),
                    'order_chgpointmoney'=>array('label'=>app::get('b2c')->_('本订单积分兑换金额'),'width'=>100),
                    'cost_freight'=>array('label'=>app::get('b2c')->_('本订单运费'),'width'=>100),
                    'pay_status'=>array('label'=>app::get('b2c')->_('支付状态'),'filtertype' => 'normal','filterdefault' => 'true','width'=>100,'type' =>
                      array (
                        0 => app::get('b2c')->_('未支付'),
                        1 => app::get('b2c')->_('已支付'),
                        2 => app::get('b2c')->_('已付款至到担保方'),
                        3 => app::get('b2c')->_('部分付款'),
                        4 => app::get('b2c')->_('部分退款'),
                        5 => app::get('b2c')->_('全额退款'),
                      )
                      ),
            );
        $this->schema = array(
            'default_in_list'=>array_keys($columns),
            'in_list'=>array_keys($columns),
            'idColumn'=>'order_id',
            'columns'=>$this->columns
        );
    }
    
    

    function get_schema(){
        return $this->schema;
    }

 
 public function _filter($filter,$tableAlias=null,$baseWhere=null){
        $where = array(1);

        foreach ($filter as $key => $value) {
            switch ($key) {
                case 'member_name':
                    $where[]="'$value' = b.login_account";
                    break;
                case 'u8code':
                    $where[]="'$value' = c.u8code";
                    break;
                case 'createstarttime':
                    $createstarttime=strtotime($value);
                    $where[]="'$createstarttime' <= a.createtime";
                    break;
                case 'createendtime':
                    $createendtime=strtotime($value." 23:59:59");
                    $where[]="'$createendtime' >= a.createtime";
                    break;
                case 'order_id':
                    $where[]="'$value' = a.order_id";
                    break;
                case 'pay_status':
                    $where[]="'$value' = a.pay_status";
                    break;
                case 'office_id':
                    $where[]="'$value' = c.office_id";
                    break;
                
            }
        }
        return implode($where,' AND ');
    }


    function getList($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null)
    {       
        $sql="select b.login_account,c.u8code
                ,a.createtime,a.order_id ,a.addon
                ,a.cost_freight ,a.pay_status,a.cost_item,c.office_id
                from sdb_b2c_orders a
                LEFT JOIN sdb_pam_members b on a.member_id=b.member_id and b.login_type='local'
                LEFT JOIN sdb_b2c_members c on c.member_id=a.member_id
                where 1=1 and a.addon is not null ";
        if($sql!='')
        {
            $sql.=' and '.$this->_filter($filter).' ORDER BY a.createtime DESC';
            $data=$this->db->selectLimit($sql,$limit,$offset);
            foreach ($data as $key=>$value)
            {
                //序列化
                $order_addon = unserialize($value['addon']);
                //积分抵扣金额：
                $data[$key]['order_chgpointmoney'] =$order_addon['order_chgpointmoney'] ? $order_addon['order_chgpointmoney'] : 0;
                //兑换积分：
                $data[$key]['order_chgpointscore'] =$order_addon['order_chgpointscore'] ? $order_addon['order_chgpointscore'] : 0;
// var_dump($data[$key]['order_chgpointmoney']);
                
                    

               $data[$key]["member_name"]=$value["login_account"];
                $data[$key]["u8code"]=$value["u8code"];
                $data[$key]["createtime"]=date("Y-m-d",$value["createtime"]);
                $data[$key]["order_id"]=$value["order_id"];
                $data[$key]["cost_freight"]=$value["cost_freight"];
                $data[$key]["pay_status"]=$value["pay_status"];
                $data[$key]["office_id"]=$value["office_id"];

                //总订单金额(不含运费、积分扣减)
                $data[$key]["cost_item"]=$value["cost_item"];
                $key++;
            }
        }
        return $data;

    
}

    function count($filter=null)
    {
        $count=0;
        $sql="select b.login_account,c.u8code
                ,a.createtime,a.order_id ,a.addon
                ,a.cost_freight ,a.pay_status,a.cost_item,c.office_id
                from sdb_b2c_orders a
                LEFT JOIN sdb_pam_members b on a.member_id=b.member_id and b.login_type='local'
                LEFT JOIN sdb_b2c_members c on c.member_id=a.member_id
                where 1=1 and a.addon is not null ";
        if($sql!='')
        {
            $sql.=' and '.$this->_filter($filter).' ORDER BY a.createtime DESC';
            $data=$this->db->select($sql);
            $count=count($data);
        }

        return $count;
    }
}
